# from PIL import Image, ImageChops
#
# # 打开两张图片
# image1 = Image.open(r'D:\hk\images\pandas\crop38.jpg')
# image2 = Image.open(r'D:\hk\images\pandas\crop40.jpg')
#
# # 比较两张图片
# diff = ImageChops.difference(image1, image2)
# print(diff)
#
# # 计算不同像素的数量
# diff_count = diff.getbbox()[2] * diff.getbbox()[3]
# print(diff_count,image1.size[0] * image1.size[1])
#
# # 计算相似度
# similarity = 1 - diff_count / float(image1.size[0] * image1.size[1])
#
# print("两张图片的相似度为：", similarity)


import cv2

# 读取图片
img1 = cv2.imread(r'D:\hk\images\pandas\crop38.jpg')
img2 = cv2.imread(r'D:\hk\images\pandas\crop40.jpg')

# 将图片转换为灰度图像
# gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
# gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

# 计算直方图
hist1 = cv2.calcHist([img1], [0], None, [256], [0, 256])
hist2 = cv2.calcHist([img2], [0], None, [256], [0, 256])

# 计算相似度
similarity = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)

print('相似度：', similarity)